data=[
    {"cat_id":1,"name":"北京","parent_id":0},
    {"cat_id":2,"name":"上海","parent_id":0},
    {"cat_id":3,"name":"沙河","parent_id":1},
    {"cat_id":4,"name":"sb镇","parent_id":3},
    {"cat_id":5,"name":"昌平","parent_id":1},
    {"cat_id":6,"name":"青浦","parent_id":2},
]
def get_tree(data):
    lists=[]
    tree={}
    for i in data:
        tree[i['cat_id']]=i
    for item in data:
        if not item['parent_id']:
            lists.append(tree[item['cat_id']])
        else:
            if "children" not in tree[item['parent_id']]:
                tree[item['parent_id']]['children']=[]
            tree[item['parent_id']]['children'].append(tree[item['cat_id']])
    print(lists)


print(get_tree(data))


